package com.unkow.first.telegram.bot;

import com.unkow.first.telegram.handler.TelegramMessageHandler;
import com.unkow.first.telegram.vo.TelBotConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

@Slf4j
public class TelPollingBotWithMsgHandler extends TelegramLongPollingBot {


    private final TelegramMessageHandler messageHandler;

    private final String botName;

    public TelPollingBotWithMsgHandler(DefaultBotOptions options, TelBotConfigVO telBotConfigVO, TelegramMessageHandler messageHandler) {
        super(options, telBotConfigVO.getBotToken());
        this.messageHandler = messageHandler;
        this.botName = telBotConfigVO.getBotUsername();
    }

    @Override
    public void onUpdateReceived(Update update) {
        log.info("Received update: {}", update);
        if (this.messageHandler != null) {
            this.messageHandler.process(this, update);
        }
    }

    @Override
    public String getBotUsername() {
        return this.botName;
    }
}
